package ignoreruleoperationlogic

import (
	"context"

	"dfx_cmdb/cmdb-blacklist-rpc/internal/svc"
	cmdb_blacklist_rpc "dfx_cmdb/cmdb-blacklist-rpc/types/cmdb-blacklist-rpc"
	cmdbblacklist "dfx_cmdb/models/cmdb-blacklist"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteIgnoreRuleLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDeleteIgnoreRuleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteIgnoreRuleLogic {
	return &DeleteIgnoreRuleLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 删除忽略规则
func (l *DeleteIgnoreRuleLogic) DeleteIgnoreRule(in *cmdb_blacklist_rpc.DeleteIgnoreRuleRequest) (*cmdb_blacklist_rpc.PublicResponse, error) {
	// 参数验证
	if in.Id <= 0 {
		return &cmdb_blacklist_rpc.PublicResponse{
			Message: "id 必须大于0",
		}, nil
	}

	// 检查规则是否存在
	var existingRule cmdbblacklist.BlacklistIgnoreRule
	if err := l.svcCtx.PDB.First(&existingRule, in.Id).Error; err != nil {
		l.Errorf("查询忽略规则失败: %v", err)
		return &cmdb_blacklist_rpc.PublicResponse{
			Message: "忽略规则不存在",
		}, nil
	}

	// 删除规则
	if err := l.svcCtx.PDB.Delete(&existingRule).Error; err != nil {
		l.Errorf("删除忽略规则失败: %v", err)
		return &cmdb_blacklist_rpc.PublicResponse{
			Message: "删除忽略规则失败",
		}, err
	}

	return &cmdb_blacklist_rpc.PublicResponse{
		Message: "删除忽略规则成功",
	}, nil
}
